<?php

namespace App\Admin\Controllers;

use App\model\Coustomer;
use App\model\CustomerFollow;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class FollowController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('跟踪记录')
            ->description('')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new CustomerFollow);
        $grid->model()->orderBy('follow_id','desc');

        $grid->image('图片')->display(function ($value){
            return "<img src='$value'>";
        });
        $grid->desc('记录');
        $grid->customer_id('客户名称')->display(function($value){
            return Coustomer::find($value)->name ?? '';
        });

        $grid->filter(function(Grid\Filter $filter){
            $filter->disableIdFilter();
            $filter->equal('customer_id','顾客id');
        });
        $grid->created_at('时间');
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(CustomerFollow::findOrFail($id));

        $show->follow_id('Follow id');
        $show->image('Image');
        $show->desc('Desc');
        $show->created_at('Created at');
        $show->updated_at('Updated at');
        $show->customer_id('Customer id');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new CustomerFollow);

        $form->image('image', 'Image');
        $form->textarea('desc', 'Desc');
        $form->number('customer_id', 'Customer id');

        return $form;
    }
}
